package com.itheima.myreflect6;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

public class MyreflectDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException {
        /*
        反射可以跟配置文件结合的方式，动态的创建对象，并调用方法
    */
        //1.获取到配置文件中的类名和方法名
        Properties prop = new Properties();
        BufferedReader br = new BufferedReader(new FileReader("myreflect\\prop.properties"));
        prop.load(br);
        br.close();
        String classname = (String) prop.get("classname");
        String method = (String) prop.get("method");

        //2.创建class字节码文件的对象
        Class<?> clazz = Class.forName(classname);

        //3.调用方法
        //获取构造方法
        Constructor<?> cons = clazz.getDeclaredConstructor();
        Object st = cons.newInstance();
        //获取成员方法并运行
        Method m = clazz.getDeclaredMethod(method);
        m.setAccessible(true);
        m.invoke(st);
    }
}
